package ihm_clarifiee.Presentation.popup;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import ihm_clarifiee.Base_Ajout_Independante.model_table_backlog;
import ihm_clarifiee.application.Histoire;
import ihm_clarifiee.application.Sprint;
import java.util.Iterator;
import javax.swing.Spring;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Popup_Ajout.java
 *
 * Created on 9 mai 2011, 21:16:39
 */
/**
 *
 * @author Simon
 */
public class Popup_Ajout_Histoire extends javax.swing.JFrame {

	//Fake attribut pour tester, ici j'ai pris un tableau de String � 2 dimensions
	
	//	- Tables � modifier
	private ArrayList<Histoire> m_listeSprint;
	private ArrayList<Histoire> m_listeBacklog;
        private ArrayList<Histoire> m_listeBacklogCurrent;
        private popup fenetre_parente;
        private Sprint sp;
	//	- Partie de l'IHM � modifier
	//private model_table_backlog m_modele_table;
	
    /** Creates new form Popup_Ajout */
    public Popup_Ajout_Histoire(ArrayList<Histoire> liste_backlog, Sprint sprint,popup fn) {
        sp=sprint;
    	m_listeSprint = sp.getlisteHistoire();
        m_listeBacklogCurrent = new ArrayList<Histoire>();
        fenetre_parente=fn;
    	m_listeBacklog = liste_backlog;
       // m_modele_table = modele_table;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        m_listeAjout = new javax.swing.JList();
        m_bt_ajouter = new javax.swing.JButton();
        m_bt_detail = new javax.swing.JButton();
        m_bt_annuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ajouter Element");
        setResizable(false);

        update();
        jScrollPane1.setViewportView(m_listeAjout);

        m_bt_ajouter.setText("Ajouter");
        m_bt_ajouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_ajouterActionPerformed(evt);
            }
        });

        m_bt_detail.setText("D�tail");

        m_bt_annuler.setText("OK");
          m_bt_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_annulerActionPerformed(evt);
            }

            private void m_bt_annulerActionPerformed(ActionEvent evt) {
              dispose();
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_bt_ajouter, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_bt_detail, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_bt_annuler, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 471, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(m_bt_ajouter)
                    .addComponent(m_bt_detail)
                    .addComponent(m_bt_annuler))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        
        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_bt_ajouter;
    private javax.swing.JButton m_bt_annuler;
    private javax.swing.JButton m_bt_detail;
    private javax.swing.JList m_listeAjout;
    // End of variables declaration//GEN-END:variables
    
    private void update()
    {
    // *****************************
        // On init ici les �l�ments de la liste d�roulante
        m_listeBacklogCurrent.clear();
        Histoire hist;
        final String[] strings = new String[m_listeBacklog.size()];
        Iterator<Histoire> it = m_listeBacklog.iterator();
        int i=0;
        while(it.hasNext())
    	{
           hist = it.next();
                if(hist.getSprintAssocie()==null)
                 {
                m_listeBacklogCurrent.add(hist);
   		strings[i] = hist.getNom();
                i++;
                 }
    	}
// *****************************
        m_listeAjout.setModel(new javax.swing.AbstractListModel() {

            // Le 'strings' pr�c�dement initialis� est utilis� ici
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
    }
    
    private void m_bt_ajouterActionPerformed(java.awt.event.ActionEvent evt) {    
    	//m_modele_table.add(m_table[m_listeAjout.getSelectedIndex()]);
        m_listeBacklogCurrent.get(m_listeAjout.getSelectedIndex()).associerSprint(sp);
        m_listeSprint.add(m_listeBacklogCurrent.get(m_listeAjout.getSelectedIndex()));
        fenetre_parente.valider();
        update();

    } 
}